"""
项目一致性校验模块路由
"""
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from pydantic import BaseModel

from ..db import get_db
from ..io_formats.validation import generate_project_checksum, validate_import_consistency

router = APIRouter(prefix="/api", tags=["一致性校验"])


class ConsistencyValidationRequest(BaseModel):
    original_project_name_en: str
    imported_project_name_en: str


@router.get("/projects/{project_name_en}/checksum")
async def get_project_checksum(project_name_en: str, db: Session = Depends(get_db)):
    """获取项目的校验哈希"""
    try:
        checksum = generate_project_checksum(project_name_en, db)
        return {"project_name_en": project_name_en, "checksum": checksum}
    except ValueError as e:
        raise HTTPException(status_code=404, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"生成校验哈希失败: {str(e)}")


@router.post("/projects/validate-consistency")
async def validate_project_consistency(
    request: ConsistencyValidationRequest,
    db: Session = Depends(get_db)
):
    """验证两个项目的一致性"""
    try:
        result = validate_import_consistency(request.original_project_name_en, request.imported_project_name_en, db)
        return result
    except ValueError as e:
        raise HTTPException(status_code=404, detail=str(e))
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"验证一致性失败: {str(e)}")

